package com.hmkj.admin.controller.realName;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.core.constant.Enum;
import com.hmkj.core.model.userAuth.UserAuthModel;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.user.User;
import com.hmkj.core.po.userAuth.UserAuth;
import com.hmkj.core.service.user.UserService;
import com.hmkj.core.service.userAuth.UserAuthService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class RealNameJsonController extends GenericController<Integer, UserAuth> {

    @Resource
    private UserAuthService userAuthService;
    @Resource
    private UserService userService;

    /**
     * 实名认证json列表
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "real/list.json", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("sys:real:list")
    public DtGrid realNameJsonList(String dtGridPager) throws Exception {
        Admin admin = AdminSessionHelper.getCurrAdmin();
        if (admin.getRoleId() == Enum.ONE_ROLE_ID || admin.getRoleId() == Enum.TWO_ROLE_ID || admin.getRoleId() == Enum.THREE_ROLE_ID || admin.getRoleId() == Enum.NEXT_SELLER_ROLE_ID) {
            User user = userService.selectByPrimaryKey(admin.getUserId());
            List<User> userList = userService.listPartnerByLevel(user.getCode(), 0);
            String userIds = "0";
            if (CollectionUtils.isNotEmpty(userList)) {
                userIds = userList.stream().map(param -> param.getId().toString()).collect(Collectors.joining(","));
            }
            ObjectMapper mapper = new ObjectMapper();
            DtGrid dtGrid1 = mapper.readValue(dtGridPager, DtGrid.class);
            Map<String, Object> dtGridFastQueryParameters = dtGrid1.getFastQueryParameters();
            dtGridFastQueryParameters.put("in_userId", userIds);
            dtGrid1.setFastQueryParameters(dtGridFastQueryParameters);
            dtGridPager = dtGrid1.toJsonString();
        }
        return userAuthService.getDtGridList(dtGridPager, UserAuthModel.class);
    }

    /**
     * 实名认证审核
     */
    @ResponseBody
    @RequestMapping(value = "userAuth/verify.json", method = RequestMethod.POST)
    @RequiresPermissions("sys:real:audit")
    public ResultEntity doVerify(UserAuth userAuth) throws Exception {
        userAuthService.doVerify(userAuth);
        return ok();
    }
}
